Most beginners start their data science journey from Jupyter notebook but they get stuck while representing their model results to non-technical people. We can’t show some fancy Python code to a non-technical person. So for that we need something more graphical and user-friendly, like a website. We can deploy our machine learning model to a web page where users can give input parameters and get the predicted value on the screen.
For this tutorial, we’ll be using Flask as a framework. We’ll take the data from the user input form on the screen and send it to the model and show the results on the user screen. The dataset which is used here is from a mental health in tech survey. It’s available on Kaggle - Dataset Link
Let’s get started. First we need to build the model. You can use any classification model for this problem. I have made a notebook for this. In that I have some data analysis as well as modeling. You can check it out on this link.
After building the model we’ll dump it into a pickle file using the following code:
1 2
import pickle pickle.dump(model, open('model.pkl', 'wb'))
Now we can access the model using this pickle file. After dumping the model we need to create a Flask web app. For front-end, we’ll use jinja2 templates. In the html page, we’ll create a form where we allow the user to give the features values for the model. After inserting the values, when the user submits these values they will go to the post method of Flask backend. Following is the code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
def ValuePredictor(to_predict_list):
to_predict = np.array(to_predict_list)
.reshape(1, 24)
print(to_predict)
loaded_model = pickle.load(open("model.pkl", "rb"))
result = loaded_model.predict(to_predict)
return result[0]
@app.route('/', methods = ['POST', 'GET'])
def result():
if request.method == 'POST':
to_predict_list = request.form.to_dict()
to_predict_list = list(to_predict_list.values())
to_predict_list = list(map(int, to_predict_list))
print(to_predict_list)
result = ValuePredictor(to_predict_list)
if int(result) == 1:
prediction = 'Yes, Leave can be given'
else:
prediction = 'No, model do not suggest to give leave'
return render_template("result.html", prediction = prediction)
else:
return flask.render_template('index.html')
Let’s understand this code in depth. We can get the input form values using the following lines of code:
1 2 3
to_predict_list = request.form.to_dict() to_predict_list = list(to_predict_list.values()) to_predict_list = list(map(int, to_predict_list))
Then we pass this list of values to a function which returns the predicted output. Our ValuePredictor function loads the model pickle file and passes the input values to its predict function.
1 2 3 4
to_predict = np.array(to_predict_list) .reshape(1, 24) loaded_model = pickle.load(open("model.pkl", "rb")) result = loaded_model.predict(to_predict)
After getting the result, we’ll render another jinja template and pass the output to it. It’ll show the output result to the end-user.
This is a simple way to represent your model to everyone. Hope you guys liked it. :)
Following is the link for complete code with model and flask app: